﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using Web.Framework.Translate;
using CrmContacto.WebApp.Code.InnerClasses;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.Contracts.Contracts.Localization;
using CrmContacto.WebApp.Code;

namespace CrmContacto.WebApp.App
{
    public partial class LenguajeList : BasePage
    {
        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton nuevoLenguajeButton = new LinkButton();
            nuevoLenguajeButton.ID = "BoLenguajeActionMenuNuevaLenguaje";
            nuevoLenguajeButton.Text = LocalizationProvider.Instance["Bo.Lenguaje.ActionMenu.NuevaLenguaje"];
            nuevoLenguajeButton.Click += new EventHandler(nuevoLenguajeButton_Click);
            actionButtons.Add(nuevoLenguajeButton);

            LinkButton refreshLenguajeButton = new LinkButton();
            refreshLenguajeButton.ID = "BoLenguajeRefreshLenguaje";
            refreshLenguajeButton.Text = LocalizationProvider.Instance["Bo.Lenguaje.ActionMenu.RefrescarLenguaje"];
            refreshLenguajeButton.Click += new EventHandler(refreshLenguajeButton_Click);
            actionButtons.Add(refreshLenguajeButton);

            base.LoadContextActions(actionButtons);
        }

        private void refreshLenguajeButton_Click(object sender, EventArgs e)
        {
            try
            {
                LocalizationHelper.ReInitialize();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void nuevoLenguajeButton_Click(object sender, EventArgs e)
        {
            AgregarNuevoLenguaje();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {                
                // HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
                if (!this.IsPostBack)
                {
                    Buscar();
                   
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected override void ApplySecurity()
        {
            //this.btnAgregarNuevo.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_CREAR);

            //if (this.GridCategorias.DataSource != null)
            //{
            //    this.GridCategorias.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
            //    this.GridCategorias.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_ELIMINAR);
            //}
        }

        private void Buscar()
        {
            //string nombreLike = this.txtNombre.Text;

            List<Lenguaje> lenguajesList = WebServiceHelper.ObtenerLenguajes();

            this.Grid.DataSource = lenguajesList;
            this.Grid.DataBind();

            pnlResults.Visible = true;
        }

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    //e.Row.Cells[1].Attributes.Add("title", "Eliminar");
                    //ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    //SetJavascriptForDeleteRecord(btnEliminarCtrl, ((Lenguaje)e.Row.DataItem).Id);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    string lenguajeId = Convert.ToString(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlLenguajeEdit + "?Id={0}", lenguajeId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    string lenguajeId = Convert.ToString(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarLenguaje(lenguajeId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.Grid.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            AgregarNuevoLenguaje();
        }

        private void AgregarNuevoLenguaje()
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlLenguajeEdit, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}